package com.csst.business.dao.mysql.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.csst.business.dao.IUserDao;
import com.csst.business.dao.TestDao;
import com.csst.business.model.Awresult;
import com.csst.business.model.BOnline;
import com.csst.business.model.BUser;
import com.csst.business.model.TNews;
import com.csst.business.model.TTrUser;
import com.csst.business.model.TUserMenu;
import com.csst.business.model.TWgroup;
import com.csst.business.model.TWuser;

public class UserDaoImpl extends BaseDaoImpl implements IUserDao {

	@Override
	public TTrUser findUserById(String id) {
		Session session=getSessionFactory().getCurrentSession();
		TTrUser user=(TTrUser)session.get(TTrUser.class, id);
		return user;
	}

	@Override
	public BUser volidate(BUser user) {
		BUser buser = new BUser();
		boolean flag = false;
		Session session=getSessionFactory().getCurrentSession();
		String hql = "from BUser b where b.userName='"+user.getUserName()+"' and b.userPass='"+user.getUserPass()+"'";
		List<BUser> list = session.createQuery(hql).list();
		if(list.size()>0){
			buser = list.get(0);
		}
		return buser;
	}

	@Override
	public void AddorUpdateUser(TWuser twuser) {
		Session session=getSessionFactory().getCurrentSession();
		TWgroup twgrop = twuser.getTWgroup();
		session.saveOrUpdate(twgrop);
		session.save(twuser);
		session.flush();
		session.clear();
	}

	@Override
	public void deleteWuserByOpenId(String openId) {
		Session session=getSessionFactory().getCurrentSession();
		
		String hql2 = "delete from TWuser t where t.WOpenid = '"+openId+"'";
		session.createQuery(hql2).executeUpdate();
		session.flush();
		session.clear();
	}

	@Override
	public int getAllUserTotle() {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from TWuser t where t.WNickname is not null  order by t.WSubscribeTime desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list.size();
	}

	@Override
	public List<TWuser> getAllWUserList(int startNum, int endNum) {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from TWuser t  where t.WNickname is not null order by t.WSubscribeTime desc";
		Query q = session.createQuery(sql);
		q.setFirstResult(startNum-1);
		q.setMaxResults(endNum);
		List<TWuser> list=q.list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public TWuser getUserByOpenId(String userOpenId) {
		TWuser buser = new TWuser();
		Session session=getSessionFactory().getCurrentSession();
		String hql = "from TWuser  t where t.WOpenid = '"+userOpenId+"'";
		List<TWuser> list = session.createQuery(hql).list();
		if(list.size()>0){
			buser = list.get(0);
		}
		return buser;
	}

	@Override
	public List<BOnline> getOnlineListByOpenId(String userOpenId) {
		Session session=getSessionFactory().getCurrentSession();
		StringBuffer sb=new StringBuffer();
		sb.append(" from BOnline t where 1=1 and t.OpenID='"+userOpenId+"'and t.isdel='1' order by t.OPretime");
		List<BOnline> list=session.createQuery(sb.toString()).list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public List<Awresult> getAwresultListByOpendId(String userOpenId) {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from Awresult t where t.userOpenId='"+userOpenId+"' order by t.addTime desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public List<TWuser> getWuserByOpenId(String fromUserName) {
		Session session=getSessionFactory().getCurrentSession();
		String hql = "from TWuser  t where t.WOpenid = '"+fromUserName+"'";
		List<TWuser> list = session.createQuery(hql).list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public void saveUser(TWuser twuser) {
		Session session=getSessionFactory().getCurrentSession();
		session.save(twuser);
		session.flush();
		session.clear();
		
	}

	@Override
	public void updateUser(TWuser twuser) {
		// TODO Auto-generated method stub
		Session session=getSessionFactory().getCurrentSession();
		session.update(twuser);
		session.flush();
		session.clear();
	}


}
